package org.jeecg.modules.demo.dtwlwjc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.dtwlwjc.entity.TFSensorWeather;
import org.jeecg.modules.demo.dtwlwjc.entity.ZwglBj;
import org.jeecg.modules.demo.dtwlwjc.mapper.TFSensorWeatherMapper;
import org.jeecg.modules.demo.dtwlwjc.mapper.ZwglBjMapper;
import org.jeecg.modules.demo.dtwlwjc.service.IZwglBjService;
import org.jeecg.modules.demo.hjyzbj.entity.ZwglEltq;
import org.jeecg.modules.demo.hjyzbj.entity.ZwglEltqsz;
import org.jeecg.modules.demo.hjyzbj.mapper.ZwglEltqMapper;
import org.jeecg.modules.demo.hjyzbj.mapper.ZwglEltqszMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Description: 报警信息
 * @Author: jeecg-boot
 * @Date: 2023-11-20
 * @Version: V1.0
 */
@Service
public class ZwglBjServiceImpl extends ServiceImpl<ZwglBjMapper, ZwglBj> implements IZwglBjService {

    @Autowired
    private TFSensorWeatherMapper tfSensorWeatherMapper;
    @Autowired
    private ZwglEltqszMapper zwglEltqszMapper;
    @Autowired
    private ZwglEltqMapper zwglEltqMapper;
    @Autowired
    private ZwglBjMapper zwglBjMapper;

    @Override
    public Result bj(Integer pageNo, Integer deviceid) {
        double gz=0;
        double wd=0;
        double sd=0;
        double zgkqwd=0;
        double zdkqwd=0;
        double zgkqsd=0;
        double zdkqsd=0;
        double zdgzqd=0;
        double zggzqd=0;
        String wj="";
        String bh="";
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);
        QueryWrapper<TFSensorWeather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deviceid", deviceid);
        queryWrapper.orderByDesc("synctime");
        queryWrapper.last("limit 1");
        List<TFSensorWeather> list = tfSensorWeatherMapper.selectList(queryWrapper);
        for (TFSensorWeather t:list){
            bh= String.valueOf(t.getDeviceid());
        }
        QueryWrapper<ZwglEltqsz> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<ZwglEltq> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.select("*");
        List<ZwglEltq> list1=zwglEltqMapper.selectList(queryWrapper2);
        for (ZwglEltq zwglEltq:list1){
            wj=zwglEltq.getId();

        }
        queryWrapper1.eq("wj", wj);
        List<ZwglEltqsz> zwglEltqszList = zwglEltqszMapper.selectList(queryWrapper1);
        if (!zwglEltqszList.isEmpty()) {
            zgkqwd = Double.parseDouble(zwglEltqszList.get(0).getKqwda());
            zdkqwd = Double.parseDouble(zwglEltqszList.get(0).getKqwdb());
            zgkqsd = Double.parseDouble(zwglEltqszList.get(0).getKqsda());
            zdkqsd = Double.parseDouble(zwglEltqszList.get(0).getKqsdb());
            zggzqd = Double.parseDouble(zwglEltqszList.get(0).getGzqda());
            zdgzqd = Double.parseDouble(zwglEltqszList.get(0).getGzqdb());
        }
        for (TFSensorWeather tfSensorWeather : list) {
            gz = tfSensorWeather.getLux();
            wd=tfSensorWeather.getTem();
            sd=tfSensorWeather.getHum();
            if (wd>zgkqwd||wd<zdkqwd) {
                ZwglBj zwglBj=new ZwglBj();
                zwglBj.setSbbh(bh);
                zwglBj.setBjlx("温度异常");
                zwglBj.setZhi(String.valueOf(wd));
                zwglBj.setBjsj(formattedDate);
                zwglBjMapper.insert(zwglBj);
            } else{
                System.out.println("无异常");
            }
            if (sd>zgkqsd||sd<zdkqsd) {
                ZwglBj zwglBj=new ZwglBj();
                zwglBj.setSbbh(bh);
                zwglBj.setBjlx("湿度异常");
                zwglBj.setZhi(String.valueOf(sd));
                zwglBj.setBjsj(formattedDate);
                zwglBjMapper.insert(zwglBj);
            }else{
                System.out.println("无异常");
            }
            if (gz>zggzqd||gz<zdgzqd) {
                ZwglBj zwglBj=new ZwglBj();
                zwglBj.setSbbh(bh);
                zwglBj.setBjlx("光照异常");
                zwglBj.setZhi(String.valueOf(gz));
                zwglBj.setBjsj(formattedDate);
                zwglBjMapper.insert(zwglBj);
            }else{
                System.out.println("无异常");
            }


        }
        return Result.OK(zwglEltqszList);
    }

    @Override
    public Result cxbj(){
        QueryWrapper<ZwglBj> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 3");
        List<ZwglBj> list=zwglBjMapper.selectList(queryWrapper);
        return Result.OK(list);
    }

}
